import {urlConfig} from "./Config";
import needle from "needle";
// const needle = require("needle");

export {getStation, getUserInfo, login, isLogged, register}

function getStation() {
    const res:{[key: string]: string[]} = {}
    for (let i = 0; i < 10; i++) {
        const region = `Region${i}`;
        res[region] = [];
        for (let j = 0; j < Math.floor(Math.random()*5+3); j++) {
            res[region].push(`${region}-Station${j}`);
        }
    }

    return res;
}

interface User {
    avatar?:string | null | undefined, // 常常应该是一个URL
    nickname: string,
    password: string,
    phone?: string,
    email?: string,
    account?: string,
    regDate?: string | Date
}
let user: null | User = null;
function getUserInfo() {
    return user;
}

async function login(account: string, password: string) {
    /*user={
        nickname: "昵称",
        password: "password",
    }
    return user;*/

    const res = await needle("get", urlConfig.getUrlFor("/user/"+account), {
        password: password
    });
    return res;
}

function register(user: User) {
    needle.post(urlConfig.getUrlFor("/user"),user)
        .on("readable", function (res) {
            console.log(res);
        })
        .on("done", function (err) {
            console.log(err);
        })
}

function isLogged() {
    return user!=null;
}
